package basis.game.main;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import javax.swing.JFrame;

public class Core extends MouseAdapter implements KeyListener{
    
    public static JFrame window;
    private int frame_timer,nFrames;
    protected int fps,width=500,height=300;
    public static int FRAMELIMIT=60;
    protected String title="Untitled";
    protected boolean running=true;
    protected Color background=new Color(0,0,0);
    
    public void init(){
        window=new JFrame(title);
        window.setSize(width,height);
        int w=Toolkit.getDefaultToolkit().getScreenSize().width;
        int h=Toolkit.getDefaultToolkit().getScreenSize().height;
        window.setLocation(w/2-window.getWidth()/2,h/2-window.getHeight()/2);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
        window.createBufferStrategy(2);
    }
    
    public void loop(){
        long c=System.currentTimeMillis();
        while(running){
            long t=System.currentTimeMillis()-c;
            c+=t;
            
            frame_timer+=t;
            nFrames++;
            if(frame_timer>=1000){
                fps=nFrames;
                frame_timer=nFrames=0;
            }
            
            update(t);
            Graphics2D g=(Graphics2D)window.getBufferStrategy().getDrawGraphics();
            render(g);
            g.dispose();
            window.getBufferStrategy().show();
            
            try{
                Thread.sleep((int)Math.round(1000./FRAMELIMIT));
            }catch(Exception e){}
        }
    }
    
    public void update(long t){
        
    }
    
    public void render(Graphics2D g){
        g.setColor(background);
        g.fillRect(0,0,width,height);
    }
    
    @Override
    public void keyPressed(KeyEvent e){
        
    }
    
    @Override
    public void keyTyped(KeyEvent e){
        
    }
    
    @Override
    public void keyReleased(KeyEvent e){
        
    }
    
}